// /** 存储类，用于存储需要本地保存的数据 */
import { _decorator, sys } from 'cc';
import { TurkishTetris_GameData } from "./TurkishTetris_GameData";
interface TurkishTetris_GameStorageData {
    guideStep_line: number;
    guideStep: number;
    high_score: number;
    GameData: any;
    isEnd: boolean,
    score: number;
}

export class TurkishTetris_GameStorage {
    static data: TurkishTetris_GameStorageData = null;
    static defaultValue: TurkishTetris_GameStorageData = null;
    static initData() {
        TurkishTetris_GameStorage.InitDefaultData();

        let dataStr = sys.localStorage.getItem(`TurkishTetris_GameStorage`);
        if (!dataStr || dataStr === "") {
            TurkishTetris_GameStorage.clearData();
        } else {
            TurkishTetris_GameStorage.data = JSON.parse(dataStr);
            TurkishTetris_GameStorage.saveData();
        }

        TurkishTetris_GameStorage.SetEveryData();
    }
    //    // 初始化默认值
    static InitDefaultData() {
        TurkishTetris_GameStorage.defaultValue = {
            guideStep: 0,
            guideStep_line: 0,
            high_score: 0,
            GameData: {},
            isEnd: true,
            score: 0,


            // high_score: 0,
            // map: {
            //     isEnd: true,
            //     score: 0,
            //     blocks: [],
            //     arrCardGroups_temp: [],
            // },
        };
    }
    // static InitMap() {
    //     TurkishTetris_GameStorage.data = {
    //         // GameData: {},
    //         isEnd: true,
    //         score: 0,
    //     }
    //     TurkishTetris_GameStorage.saveData();
    // }
    static SetEveryData() {
        // 递归检查并更新所有属性
        TurkishTetris_GameStorage.mergeDataRecursively(TurkishTetris_GameStorage.data, TurkishTetris_GameStorage.defaultValue);
        TurkishTetris_GameStorage.saveData();
    }
    //    // 递归合并数据，确保所有属性都存在
    static mergeDataRecursively(target: any, source: any) {
        for (let key in source) {
            if (source.hasOwnProperty(key)) {
                // 如果目标对象没有这个属性，直接赋值
                if (!target.hasOwnProperty(key)) {
                    target[key] = source[key];
                }
                // 如果目标对象有这个属性，且源对象的值是对象类型，则递归检查
                else if (typeof source[key] === 'object' && source[key] !== null && !Array.isArray(source[key])) {
                    if (typeof target[key] === 'object' && target[key] !== null && !Array.isArray(target[key])) {
                        TurkishTetris_GameStorage.mergeDataRecursively(target[key], source[key]);
                    } else {
                        // 如果目标不是对象类型，用默认值替换
                        target[key] = source[key];
                    }
                }
                // 对于数组类型，如果目标没有这个属性，则使用默认值
                else if (Array.isArray(source[key]) && !target.hasOwnProperty(key)) {
                    target[key] = source[key];
                }
            }
        }
    }
    static clearData() {
        TurkishTetris_GameStorage.data = { ...TurkishTetris_GameStorage.defaultValue };
    }
    static saveData() {
        let dataStr = JSON.stringify(TurkishTetris_GameStorage.data);
        sys.localStorage.setItem(`TurkishTetris_GameStorage`, dataStr);
    }
    static ArrCardGroupsToMapData() {
        if (TurkishTetris_GameData.gameMode != 0) {
            return;
        }
        let data = [];

        for (let y = 0; y < TurkishTetris_GameData.arrCardGroups.length; y++) {
            data[y] = [];
            const row = TurkishTetris_GameData.arrCardGroups[y];
            for (let index = 0; index < row.length; index++) {
                const cardGroup = row[index];
                let newData = cardGroup.ToMapData();
                data[y][index] = newData;
            }
        }
        // this.data.mapData = data;
        TurkishTetris_GameStorage.data.GameData.mapData = data;
        // console.log(`data111`, data);

        let data2 = [];
        for (let index = 0; index < TurkishTetris_GameData.arrCardGroups_next.length; index++) {
            const cardGroup = TurkishTetris_GameData.arrCardGroups_next[index];
            let newData = cardGroup.ToMapData();
            data2[index] = newData;
        }
        TurkishTetris_GameStorage.data.GameData.mapData_next = data2;

        TurkishTetris_GameStorage.saveData();

        return data;
    }
}

window["TurkishTetris_GameStorage"] = TurkishTetris_GameStorage;